Lists
- List Basics Overview:
- Five Basic Concepts for a New Data Type:
- Displaying Lists:
- Creating Lists:
- Common List Operations:
- List Transformations Overview:
- Transformation through
map
: - Transformation Flexibility:
- Transforming Other Types of Data:
- Errors and Type Matching:
- Using
apply
: - Dealing with Complex Data:
- Error Handling in
map
andapply
: - Summary:
List Basics Overview:
In Scheme (and Scamper), a list is a fundamental data structure that holds collections of values. Lists are essential for handling multiple data points at once, making them especially useful in more complex problems, such as those found in data science.
The document introduces the basic concepts of lists in Scheme, how to create lists, and the operations that can be performed on them.
Five Basic Concepts for a New Data Type:
- Name: The data type is called a list.
- Purpose: The main purpose of a list is to group or collect values.
- How to Express Values: You can create and manipulate lists using specific functions.
- Displaying Values: Lists are displayed either as function calls (in Scamper) or as parenthesized lists in Scheme.
- Operations Available: A variety of functions can be used to manipulate and build lists.
Displaying Lists:
-
Lists are created using the
list
function. For example:(list 2 12 32)
This will create a list with the values
2
,12
, and32
. -
Example of converting a string to a list:
(string->list "hello world!")
This converts the string into a list of individual characters:
(#\h #\e #\l #\l #\o #\space #\w #\o #\r #\l #\d #\!)
Creating Lists:
-
Using the
list
function:(list (+ 1 1) (string-length "hello world!") 32)
This will create a list with the results of expressions:
2
,12
, and32
. -
make-list
function:
Creates a list of identical values:(make-list 5 "hello")
This produces a list with five "hello" strings:
("hello" "hello" "hello" "hello" "hello")
-
range
function:
Produces a list of numbers. It can take one, two, or three arguments to control the start, end, and step of the range:(range 7) ; produces (0 1 2 3 4 5 6) (range 1 10 2) ; produces (1 3 5 7 9) (range 13 -5 -3) ; produces (13 10 7 4 1 -2)
Common List Operations:
-
length
: Returns the number of elements in the list:(length (list 3 4 5)) ; produces 3
-
list-ref
: Accesses an element of the list at a given index (0-based):(list-ref (list 1 2 3 4) 2) ; produces 3
-
index-of
: Returns the position of the first occurrence of a value in a list:(index-of (list "a" "b" "c") "b") ; produces 1
-
reverse
: Reverses the list:(reverse (list 1 2 3 4 5)) ; produces (5 4 3 2 1)
-
append
: Combines two lists into one:(append (list 1 2) (list 3 4)) ; produces (1 2 3 4)
-
list-take
andlist-drop
:list-take
: Takes the first n elements from a list.(list-take (list 1 2 3 4 5) 3) ; produces (1 2 3)
list-drop
: Drops the first n elements from a list.(list-drop (list 1 2 3 4 5) 2) ; produces (3 4 5)
Lists in Scheme provide a versatile way to group and manage data. You can create lists with a wide variety of functions, manipulate them with operations like append
, reverse
, list-ref
, and more, and use them to handle sequences of data efficiently.
List Transformations Overview:
In computing, list transformations involve transforming individual elements of a collection, which can be efficiently done using higher-order functions like map
and apply
. This document explains how transformations over collections (lists) are central to functional programming and how they can be performed in Scheme.
Transformation through map
:
The map
function is a higher-order function in Scheme that transforms each element of a list by applying a given function to every element. It takes two arguments:
- A function that transforms a single element.
- A list of elements to transform.
For example, if you have a function that adjusts salaries based on a cost-of-living adjustment (COLA):
(define compute-cola-salary
(lambda (salary)
(+ salary (* salary 0.016))))
You can use map
to apply this function to a list of salaries:
(map compute-cola-salary (list 100000 100000 50000 75000 500000))
This produces:
(list 101600 101600 50800 76200 508000)
Transformation Flexibility:
- The
map
function does not affect the original list. Instead, it generates a new list with transformed elements. - You can use
map
to apply any function, for example, doubling salaries:(define double-salary (lambda (salary) (* salary 2))) (map double-salary salaries) ; produces (200000 200000 100000 150000 1000000)
Transforming Other Types of Data:
map
can transform any collection, not just numbers. For example:
- To convert a list of strings to uppercase:
(map string-upcase (list "please" "be" "quiet" "not" "loud")) ; produces ("PLEASE" "BE" "QUIET" "NOT" "LOUD")
Errors and Type Matching:
- When using
map
, the types of elements in the list must match the type expected by the function. For example, applyingsquare
to a list of strings will result in an error becausesquare
expects numbers.(map square (list "please" "be" "quiet")) ; produces a type error
Using apply
:
While map
applies a function to each element of a list, apply
takes a procedure and applies it to an entire list of arguments at once.
- Example of using
apply
to sum a list of numbers:(apply + (list 1 2 3 4 5)) ; produces 15
- Similarly, you can use
apply
for other functions likestring-append
:(apply string-append (list "this" "and" "that")) ; produces "thisandthat"
Dealing with Complex Data:
Sometimes transformations involve more complex collections like images. Consider transforming a list of numbers into a series of green circles:
(define green-circle
(lambda (radius)
(circle radius "solid" "green")))
(map green-circle (list 20 40 60 40 20))
This produces a list of circles with different radii, which you can then combine using functions like beside
or above
.
Error Handling in map
and apply
:
- If you use incorrect types, both
map
andapply
will generate clear error messages. For example, trying to use+
with a list of strings will raise an error.
Summary:
List transformations are a powerful tool in functional programming. The map
function is central to transforming individual elements of a list based on a defined function. Additionally, apply
allows you to apply a function to an entire list at once. These transformations are crucial for efficiently manipulating collections of data.